package com.duan.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.duan.goods.mapper.ClassifyMapper;
import com.duan.goods.mapper.ProductMapper;
import com.duan.goods.mapper.SpecMapper;
import com.duan.goods.pojo.*;
import com.duan.goods.service.DetailService;
import com.duan.goods.service.ProductDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DetailServiceImpl implements DetailService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ClassifyMapper classifyMapper;
    @Autowired
    private ProductDetailsService productDetailsService;
    @Autowired
    private SpecMapper specMapper;

    @Override
    public Detail findDetail(Long id) {

        Detail detail=new Detail();
        //根据id查询商品对象
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getId, id);
        lqw.eq(Product::getIsDelete, 0);
        Product product = productMapper.selectOne(lqw);
        Classify classify = this.findClassify(product.getClassifyId());
        //分类对象名称
        StringBuilder classifyName = new StringBuilder();
        if (classify != null) {
            classifyName.append(classify.getClassifyName());
            //如果有上级id，继续查询
            if (classify.getIsuperiorId() != 0) {
                Classify classify1 = this.findClassify(classify.getIsuperiorId());
                classifyName.insert(0, classify1.getClassifyName() + ">");
                //如果有上级id，继续查询
                if (classify1.getIsuperiorId() != 0) {
                    Classify classify2 = this.findClassify(classify1.getIsuperiorId());
                    classifyName.insert(0, classify2.getClassifyName() + ">");
                }
            }
        }
        //把specid转换成specname
        ProductDetails p = new ProductDetails();
        p.setProductId(id);
        p.setIsDelete(0);
        List<ProductDetails> list = productDetailsService.selectList(p);
        for (ProductDetails productDetails : list) {
            String specId = productDetails.getSpecId();
            String[] split = specId.split(",");
            String name = "";
            int count = 0;
            for (String s : split) {
                Spec spec = specMapper.selectById(Long.valueOf(s));
                if (count == 0) {
                    name = name + spec.getSpecValue();
                } else {
                    name = name + " " + spec.getSpecValue();
                }
                count++;
            }
            productDetails.setSpecId(name);
        }
        //基本信息
        detail.setId(product.getId());
        detail.setCategory(classifyName.toString());
        detail.setName(product.getName());
        detail.setSelling(product.getSelling());
        detail.setParameter(product.getParameter());
        //商品交易信息
        detail.setUnitMeasure(product.getUnitMeasure());
        detail.setSalesModel(product.getSalesModel());
        //商品规格及图片
        detail.setPrice(product.getPrice().doubleValue());
        detail.setImg(product.getImg());
        detail.setVideo(product.getVideo());
        Map map= new HashMap<>();
        map.put("spec",list);
        detail.setSpec(map);
        //商品详情描述
        detail.setDescription(product.getDescription());
        return detail;
    }

    //id查询分类对象
    public Classify findClassify(Long id) {
        LambdaQueryWrapper<Classify> lqw = new LambdaQueryWrapper();
        lqw.eq(Classify::getId, id);
        lqw.eq(Classify::getIsDelete, 0);
        Classify classify = classifyMapper.selectOne(lqw);
        return classify;
    }
}

